package cn.itcast.bos.web.action.take_delivery;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.service.take_delivery.WayBillService;
import cn.itcast.bos.web.action.common.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class WayBillAction extends BaseAction<WayBill> {
	// 日志记录
	private static final Logger LOGGER = Logger.getLogger(WayBillAction.class);
	@Autowired
	private WayBillService wayBillService;

	@Action(value = "waybill_save", results = { @Result(name = "success", type = "json") })
	public String save() {
		//去除没有orderid的订单
		if(model.getOrder() !=null &&(model.getOrder().getId()==null||model.getOrder().getId()==0)){
			model.setOrder(null);
		}
		

		Map<String, Object> result = new HashMap<String, Object>();
		try {

			wayBillService.save(model);
			result.put("success", true);
			result.put("msg", "运单快速录入，保存成功");
			LOGGER.info("保存运单成功，运单号：" + model.getWayBillNum());
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "运单快速录入失败");
			LOGGER.info("保存运单失败，运单号：" + model.getWayBillNum() + e.getMessage());
		}
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}

	@Action(value = "waybill_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		Pageable pageable = new PageRequest(page - 1, rows
				/*new Sort(new Sort.Order(Sort.Direction.DESC, "id"))*/);
		System.out.println(model+"     "+pageable.toString()+"====================================");
		Page<WayBill> pageData = wayBillService.findPageData(model, pageable);
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}
	
	@Action(value="wayBill_findByWayBillNum", results = { @Result(name = "success", type = "json") })
	public String findByWayBillNum(){
		WayBill wayBill = wayBillService.findByWayBillNum(model.getWayBillNum());
		Map<String, Object> result = new HashMap<String, Object>();
		if(wayBill == null){
			//没有查询到订单
			result.put("success", false);
		}else{
			//查询到订单
			result.put("success", true);
			result.put("wayBillNumData", wayBill);
		}
		//压入值栈
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}
}
